package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    String msg;

    // 用户登录
    @Override
    public String loginByUP(User user) {
        // 获取密码
        byte[] bytes = user.getPassword().getBytes();
        // 把密码转换成md5格式
        String password = DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(password);
        // 去数据库查找
        Integer userDB = userMapper.loginByUP(user);
        if (userDB == 0) {
            return null;
        }
        // 成功返回 token 是一串UUID字符串
        String token = UUID.randomUUID().toString().replace("-", "");
        return token;
    }

    // 用户注册
    @Override
    public String register(User user) {
        // 校验手机号和身份证号，邮箱是否位正常号码
        if(!user.getPhone().matches(
                "^1([38][0-9]|4[5-9]|5[0-3,5-9]|66|7[0-8]|9[89])[0-9]{8}$") ||
                !user.getIdCard().matches("\\d{17}(\\d|X|x)") ||
                !user.getEmail().matches("^[a-zA-Z0-9-_]+@[a-zA-Z0-9-_]+\\.[a-zA-Z0-9-_]+$")
        ){

            return "填写信息有误";
        }
        // 把密码加密
        byte[] bytes = user.getPassword().getBytes();
        String ps = DigestUtils.md5DigestAsHex(bytes);
        // 获取当前时间
        Date date = new Date();
        user.setPassword(ps).setStatus(false).setCreated(date).setUpdated(date);

        Integer userDB = userMapper.findUn(user.getUsername());
        if(userDB > 0){

            return "用户名已存在";
        }
        userMapper.register(user);
        return null;
    }

    // 密码修改
    @Override
    public String updateByUn(User user) {
        // 把密码加密
        byte[] bytes = user.getPassword().getBytes();
        String ps = DigestUtils.md5DigestAsHex(bytes);

        user.setPassword(ps).setUpdated(new Date());
        Integer id = userMapper.findUPI(user);
        if(id == 0){
            return "填写的信息有误";
        }
        user.setId(id);
        userMapper.updateByUn(user);
        return null;
    }

    // 分页查询
    @Override
    public PageResult getUserListByPage(PageResult pageResult) {
        // 总数
        long total = userMapper.findTotal();
        // 分页
        int size = pageResult.getPageSize(); // 条数
        int start = (pageResult.getPageNum() -1 )*size; // 起始位置
        String query = pageResult.getQuery(); // 查询条件
        List<User> userList = userMapper.findUserListByPage(start,size,query);

        return pageResult.setTotal(total).setRows(userList);
    }

    // 根据id改状态
    @Override
    public String updateStatusById(User user) {
        int id = userMapper.findId(user.getId());
        if(id == 0){
            return "没有此Id";
        }
        userMapper.updateStatusById(user);
        return null;
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public String updateUser(User user) {
        if(
                (user.getAlias() == null || "".equals(user.getAlias()))
                || (user.getEmail() == null || "".equals(user.getEmail()))
                || (user.getPhone() == null || "".equals(user.getPhone()))){
            return "数据不能为空";
        }
        user.setUpdated(new Date());
        userMapper.updateUser(user);
        return null;
    }

    @Transactional
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }
}
